package com.sambo.inspection.util;

import java.util.Calendar;

public class DateUtil {

    public static long dateTime2TS(String datetime) {
        // 解析yyyy-MM-dd hh:mm:ss格式的字符串
        String[] datetimeArray = datetime.split(" ");

        if (datetimeArray.length != 2) {
            return 0;
        }

        String[] dates = datetimeArray[0].split("-");

        if (dates.length != 3) {
            return 0;
        }

        String[] times = datetimeArray[1].split(":");

        if (times.length != 3) {
            return 0;
        }

        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, Integer.parseInt(dates[0]));
            calendar.set(Calendar.MONTH, Integer.parseInt(dates[1]));
            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dates[2]));
            calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0]));
            calendar.set(Calendar.MINUTE, Integer.parseInt(times[1]));
            calendar.set(Calendar.SECOND, Integer.parseInt(times[2]));
            calendar.set(Calendar.MILLISECOND, 0);

            return calendar.getTimeInMillis();
        } catch (Throwable e) {
            return 0;
        }

    }



}
